<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>学生成绩管理系统 - 学生管理</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 100;
            padding: 48px 0 0;
            box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
            background-color: #f8f9fa;
        }
        .sidebar-sticky {
            position: relative;
            top: 0;
            height: calc(100vh - 48px);
            padding-top: .5rem;
            overflow-x: hidden;
            overflow-y: auto;
        }
        .navbar {
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .main-content {
            margin-left: 240px;
            padding: 20px;
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
        }
        .nav-link:hover {
            background-color: #e9ecef;
        }
        .nav-link.active {
            background-color: #007bff;
            color: white;
        }
        .nav-link i {
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">学生成绩管理系统</a>
            <div class="d-flex">
                <span class="navbar-text me-3">
                    欢迎，${sessionScope.user.username}
                </span>
                <button class="btn btn-outline-light" onclick="logout()">退出</button>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <nav class="col-md-3 col-lg-2 d-md-block sidebar">
                <div class="sidebar-sticky">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="index.jsp">
                                <i class="fas fa-home"></i>首页
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="student.jsp">
                                <i class="fas fa-user-graduate"></i>学生管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="teacher.jsp">
                                <i class="fas fa-chalkboard-teacher"></i>教师管理
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>

            <main class="main-content">
                <div class="container">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h2>学生管理</h2>
                        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStudentModal">
                            <i class="fas fa-plus"></i> 添加学生
                        </button>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>学号</th>
                                            <th>中文名</th>
                                            <th>英文名</th>
                                            <th>电话</th>
                                            <th>班级</th>
                                            <th>学期</th>
                                            <th>期中成绩</th>
                                            <th>期末成绩</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="studentTableBody">
                                        <!-- 学生数据将通过JavaScript动态加载 -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- 添加学生模态框 -->
    <div class="modal fade" id="addStudentModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">添加学生</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="addStudentForm">
                        <div class="mb-3">
                            <label class="form-label">学号</label>
                            <input type="text" class="form-control" name="studentId" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">中文名</label>
                            <input type="text" class="form-control" name="chineseName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">英文名</label>
                            <input type="text" class="form-control" name="englishName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">电话</label>
                            <input type="tel" class="form-control" name="tel" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">班级</label>
                            <input type="text" class="form-control" name="stuClass" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">学期</label>
                            <input type="number" class="form-control" name="semester" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">状态</label>
                            <select class="form-control" name="stuStatus" required>
                                <option value="A">在读</option>
                                <option value="L">离校</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">期中成绩</label>
                            <input type="number" step="0.01" class="form-control" name="midMarks" value="0">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">期末成绩</label>
                            <input type="number" step="0.01" class="form-control" name="endMarks" value="0">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="addStudent()">添加</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑学生模态框 -->
    <div class="modal fade" id="editStudentModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑学生</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="editStudentForm">
                        <input type="hidden" name="studentId">
                        <div class="mb-3">
                            <label class="form-label">中文名</label>
                            <input type="text" class="form-control" name="chineseName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">英文名</label>
                            <input type="text" class="form-control" name="englishName" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">电话</label>
                            <input type="tel" class="form-control" name="tel" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">班级</label>
                            <input type="text" class="form-control" name="stuClass" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">学期</label>
                            <input type="number" class="form-control" name="semester" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">状态</label>
                            <select class="form-control" name="stuStatus" required>
                                <option value="A">在读</option>
                                <option value="L">离校</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">期中成绩</label>
                            <input type="number" step="0.01" class="form-control" name="midMarks">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">期末成绩</label>
                            <input type="number" step="0.01" class="form-control" name="endMarks">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="updateStudent()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        function logout() {
            $.post('${pageContext.request.contextPath}/user/logout', function() {
                window.location.href = '${pageContext.request.contextPath}/login.jsp';
            });
        }

        // 加载学生列表
        function loadStudents() {
            $.get('${pageContext.request.contextPath}/student/list', function(data) {
                console.log('获取到的数据:', data);
                const tbody = $('#studentTableBody');
                tbody.empty();
                
                if (!data || data.length === 0) {
                    console.log('没有数据');
                    return;
                }

                data.forEach(function(student) {
                    console.log('处理学生数据:', student);
                    const row = '<tr>' +
                        '<td>' + student.studentId + '</td>' +
                        '<td>' + student.chineseName + '</td>' +
                        '<td>' + student.englishName + '</td>' +
                        '<td>' + student.tel + '</td>' +
                        '<td>' + student.stuClass + '</td>' +
                        '<td>' + student.semester + '</td>' +
                        '<td>' + (student.midMarks != null ? student.midMarks.toFixed(2) : '-') + '</td>' +
                        '<td>' + (student.endMarks != null ? student.endMarks.toFixed(2) : '-') + '</td>' +
                        '<td>' + (student.stuStatus == 'A' ? '在读' : '离校') + '</td>' +
                        '<td>' +
                        '<button class="btn btn-sm btn-primary" onclick="editStudent(\'' + student.studentId + '\')">' +
                        '编辑' +
                        '</button> ' +
                        '<button class="btn btn-sm btn-danger" onclick="deleteStudent(\'' + student.studentId + '\')">' +
                        '删除' +
                        '</button>' +
                        '</td>' +
                        '</tr>';
                    console.log('生成的HTML:', row);
                    tbody.append(row);
                });
            }).fail(function(jqXHR, textStatus, errorThrown) {
                console.error('加载学生列表失败:', textStatus, errorThrown);
                alert('加载学生列表失败，请检查控制台获取详细信息');
            });
        }

        // 添加学生
        function addStudent() {
            const formData = {};
            $('#addStudentForm').serializeArray().forEach(function(item) {
                formData[item.name] = item.value;
            });
            
            $.ajax({
                url: '${pageContext.request.contextPath}/student',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(response) {
                    if (response.success) {
                        $('#addStudentModal').modal('hide');
                        loadStudents();
                        alert('添加成功');
                    } else {
                        alert(response.message || '添加失败');
                    }
                }
            });
        }

        // 编辑学生
        function editStudent(studentId) {
            $.get('${pageContext.request.contextPath}/student/' + studentId, function(student) {
                const form = $('#editStudentForm');
                form.find('[name=studentId]').val(student.studentId);
                form.find('[name=chineseName]').val(student.chineseName);
                form.find('[name=englishName]').val(student.englishName);
                form.find('[name=tel]').val(student.tel);
                form.find('[name=stuClass]').val(student.stuClass);
                form.find('[name=semester]').val(student.semester);
                form.find('[name=stuStatus]').val(student.stuStatus);
                form.find('[name=midMarks]').val(student.midMarks);
                form.find('[name=endMarks]').val(student.endMarks);
                $('#editStudentModal').modal('show');
            });
        }

        // 更新学生
        function updateStudent() {
            const formData = {};
            $('#editStudentForm').serializeArray().forEach(function(item) {
                formData[item.name] = item.value;
            });
            
            $.ajax({
                url: '${pageContext.request.contextPath}/student',
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(response) {
                    if (response.success) {
                        $('#editStudentModal').modal('hide');
                        loadStudents();
                        alert('更新成功');
                    } else {
                        alert(response.message || '更新失败');
                    }
                }
            });
        }

        // 删除学生
        function deleteStudent(studentId) {
            if (confirm('确定要删除这个学生吗？')) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/student/' + studentId,
                    type: 'DELETE',
                    success: function(response) {
                        if (response.success) {
                            loadStudents();
                            alert('删除成功');
                        } else {
                            alert(response.message || '删除失败');
                        }
                    }
                });
            }
        }

        // 页面加载完成后加载学生列表
        $(document).ready(function() {
            loadStudents();
        });
    </script>
</body>
</html> 